from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
from datetime import datetime
class Base():
    def __init__(self,driver):
        self.driver=driver
    #浏览器事件
    def goto(self,url):
        self.driver.get(url)
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver:driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            time=datetime.now().strftime("Y%m%d_%H%M%S")
            self.driver.save_screenshot(f"tieba{time}.png")
            print(e)
    #鼠标事件
    def click(self,loc):
        self.find_element(loc).click()
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    def clear(self,loc):
        self.find_element(loc).clear()
    def text(self,loc):
       return self.find_element(loc).text
    #键盘事件
    def ENTER(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)
    #切换页面
    def qiehuan(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    #滚动条（执行js代码）
    def gundong(self):
        self.driver.execute_script("window scrollTo(0,document.body.Height)")
    #下拉框选择
    def xiala_value(self,x):
        ss=Select(self.find_element(x))
        ss.select_by_value(x)
    #弹框处理
    def alert(self,ss):
        self.driver.switch_to.alert(ss)
    #iframe处理
    def fname(self,info):
        self.driver.switch_to.frame(info)